$(function () {
    $("#showAddBtn").click(function() {
        $("#sectionNameNew, #addSectionBtn").toggle();
    });

    $("#searchForm").submit(function (event) {
        event.preventDefault(); // 阻止表单默认提交行为

        var username = $("#userName").val();
        var sectionName = $("#sectionName").val();
        var status = $("#status").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();

        $.ajax({
            url: "/section/searchSections",
            type: "POST",
            data: {
                "username": username,
                "sectionName": sectionName,
                "status": status,
                "startTime": startTime,
                "endTime": endTime
            },
            success: function (data){
                $("#dataContent").html(data);
            }
        })
    })
})
function showForm(sectionId){
    $("#hintModal").modal("show");
    $("#candidateSection").val(sectionId);
}
function changeStatus(sectionId, targetStatus){
    $.ajax({
        url:"/section/changeStatus",
        type: "POST",
        data:{"sectionId": sectionId ,"status": targetStatus},
        success: function (data){
            $("#dataContent").html(data);
        }
    })
}

function searchUser() {
    var candidateName = $("#candidateName").val();
    if (candidateName == null){
        console.log("不能为空");
    }
    $.ajax({
        url: "/user/searchCandidate",
        type: "POST",
        data: {
            "candidateName":candidateName,
        },
        success: function (data){
            var content = $("#candidateBody");
            content.empty();
            $.each(data, function(index, user) {
                var rowHtml = "<tr>" +
                    "<td><img src='" + user.headerUrl + "' class='rounded-circle' style='width: 30px'>&nbsp;" + user.username + "</td>" +
                    "<td><button type='button' class='btn btn-primary btn-sm' onclick='changeModerator("+user.id+")'>选择</button></td>" +
                    "</tr>";
                content.append(rowHtml);
            });

        }
    })
}
function changeModerator(userId){
    var sectionId = $("#candidateSection").val();
    $.ajax({
        url:"/section/changeModerator",
        type: "POST",
        data:{"sectionId": sectionId ,"userId": userId},
        success: function (data){
            showAlert("成功");
            $("#hintModal").modal("dispose");
            location.reload();
        }
    })
}

function addSection(){
    var name = $("#sectionNameNew").val();
    if (name === "" || name == null){
        showAlert("不能为空！");
    }
    $.ajax({
        type: "POST",
        url: "/section/addSection",
        data: {"name":name},
        success:function (data){
            $("#sectionNameNew, #addSectionBtn").toggle();
            $("#dataContent").html(data);
        }
    })
}

function clearSearch() {
    // 获取表单元素
    var form = document.getElementById("searchForm");

    // 将文本框和下拉框的值设置为空字符串或默认值
    form.sectionName.value = "";
    form.userName.value = "";
    form.status.value = "999";
    form.startTime.value = "";
    form.endTime.value = "";
}